package com.xca.photogallery.base;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.photogallery.common.CustomToast;
import com.xca.photogallery.common.LoadingDialog;

public abstract class BaseActivity extends AppCompatActivity {
    private static final int FAST_CLICK_INTERVAL = 500;//快速点击间隔
    private long lastClickTime;//last click time
    private CustomToast mToast;
    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//全局只能竖屏
        setContentView(getLayoutId());
        init();
    }

    public abstract int getLayoutId();

    public abstract void init();

    public void showToast(int id) {
        String ssdf = getString(id);
        showToast(ssdf);
    }

    public void showToast(String text) {
        if (!TextUtils.isEmpty(text)) {
            if (mToast == null) {
                mToast = new CustomToast(this);
            }
            mToast.setDuration(Toast.LENGTH_LONG);
            mToast.setText(text);
            mToast.show();
        }
    }

    /**
     * 判断是否是快速点击
     *
     * @return
     */
    public boolean isFastClick() {
        long now = System.currentTimeMillis();
        if (now - lastClickTime < FAST_CLICK_INTERVAL) {
            return true;
        }
        lastClickTime = now;
        return false;
    }

    /**
     * 显示正在加载弹框
     */
    public void showLoading() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.show();
    }

    /**
     * 隐藏正在加载弹框
     */
    public void dismissLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }
}
